/**
 * Description:
 * Class Name:DrawThread
 * Date:2022/5/6 14:13
 *
 * @author LTisme
 * @version JAVA17
 */
package 疯狂Java讲义.第16章_多线程._16_5_同步线程._16_5_1_线程安全问题之银行取钱问题;

public class DrawThread extends Thread{
    // 模拟用户账户
    private Account account;
    // 当前线程希望的取钱数
    private double drawAmount;
    public DrawThread(String name, Account account, double drawAmount){
        super(name);
        this.account = account;
        this.drawAmount = drawAmount;
    }

    // 当多个线程修改同一个共享数据时，将涉及数据安全问题
    @Override
    public void run() {
        // 账户余额大于取钱数目，则可取钱
        if (account.getBalance() >= drawAmount){
            // 吐出钞票
            System.out.println(getName() + "取钱成功！吐出钞票：" + drawAmount + " 元");
            try {
                Thread.sleep(1);
            }
            catch (InterruptedException e){
                e.printStackTrace();
            }
            // 修改余额
            account.setBalance(account.getBalance() - drawAmount);
            System.out.println("\t余额为：" + account.getBalance());
        }
        else {
            System.out.println(getName() + " 抱歉，余额不足！");
        }
    }
}
